// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   DefaultFileCleanPolicy.java

package com.hfstu.file.utils;

import com.hfstu.file.dataaccess.service.IUploadFileService;
import com.hfstu.file.server.FileServerContext;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

// Referenced classes of package com.hfstu.file.utils:
//			FileCleanPolicy

public class DefaultFileCleanPolicy
	implements FileCleanPolicy
{

	private Logger _log;

	public DefaultFileCleanPolicy()
	{
		_log = LoggerFactory.getLogger(com/hfstu/file/utils/DefaultFileCleanPolicy);
	}

	public void clean(String filePath)
	{
		File file = new File(filePath);
		fileClean(file);
	}

	private void fileClean(File file)
	{
		if (!file.exists())
			return;
		if (file.isFile())
		{
			String id = getFileId(file);
			if (FileServerContext.getInstance().getUploadFileService().getById(id) == null)
				if (!file.delete())
					_log.warn((new StringBuilder("remove file failure. file id is ")).append(id).toString());
				else
					_log.info((new StringBuilder("remove file success. file id is ")).append(id).toString());
		} else
		if (file.isDirectory())
		{
			File subFiles[] = file.listFiles();
			File afile[];
			int j = (afile = subFiles).length;
			for (int i = 0; i < j; i++)
			{
				File tmpFile = afile[i];
				fileClean(tmpFile);
			}

		}
	}

	private String getFileId(File file)
	{
		if (file.isFile())
		{
			String fileName = file.getName().trim();
			int endIndex = fileName.lastIndexOf(".");
			if (fileName.length() > 0 && endIndex > 0)
				return fileName.substring(0, endIndex);
			else
				return "";
		} else
		{
			throw new IllegalArgumentException((new StringBuilder(String.valueOf(file.getName()))).append(" is not a file.").toString());
		}
	}
}
